﻿@page "/auth"
@using System.Collections.ObjectModel
@inject RBACService RBACService
@inject DialogService DialogService
@inject NotificationService NotificationService

<RadzenText TextStyle="TextStyle.H4">权限管理</RadzenText>
<PageTitle>权限管理</PageTitle>
<RadzenStack Style="display: flex; margin: 0px 10px 10px 0px" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Start" AlignItems="AlignItems.Center">
    <RadzenTextBox @bind-Value="@keyword" Placeholder="权限名称/唯一编码" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="查询" Icon="refresh" Click=@(args => Search()) ButtonStyle="ButtonStyle.Primary" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="新增" Icon="add_circle" Click=@(args => Add()) ButtonStyle="ButtonStyle.Primary" Style="margin:0px" />
</RadzenStack>
<RadzenDataGrid EmptyText="暂无数据" Data="@auths" GridLines="DataGridGridLines.Default" AllowPaging="true" AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn Property="Id" Title="序号" Width="100px" TextAlign="TextAlign.Center" />
        <RadzenDataGridColumn Property="" Title="操作" Width="160px">
            <Template Context="data">
                <RadzenStack Gap="10" Style="margin-bottom: 10px" Orientation="Orientation.Horizontal">
                    <RadzenButton Text="修改" @onclick:stopPropagation Click=@(args => Update(data)) ButtonStyle="ButtonStyle.Primary" />
                    <RadzenButton Text="删除" @onclick:stopPropagation Click=@(args => Delete(data)) ButtonStyle="ButtonStyle.Danger" />
                </RadzenStack>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="UniqueName" Title="权限名称" />
        <RadzenDataGridColumn Property="UniqueCode" Title="唯一编码" />
    </Columns>
</RadzenDataGrid>

@code {
    string keyword = default!;
    ObservableCollection<Permission> auths = default!;

    private void Search()
    {
        Query();
    }

    private void Query()
    {
        var input = new SimpleInput(keyword);
        var list = RBACService.GetAuthList(input);
        auths = new ObservableCollection<Permission>(list);
    }

    private async void Add()
    {
        Permission result = await DialogService.OpenAsync<AuthAddDialog>($"新增角色",
               new Dictionary<string, object>() { { "PPermission", new Permission() } },
               new DialogOptions()
                   {
                       Resizable = true,
                       Draggable = true,
                       Width = "700px",
                       Height = "512px",
                       Left = null,
                       Top = null
                   });
        if (result != null)
        {
            auths.Add(result);
        }
    }

    private async void Update(Permission selectedItem)
    {
        Permission result = await DialogService.OpenAsync<AuthAddDialog>($"修改权限",
            new Dictionary<string, object>() { { "PPermission", selectedItem } },
            new DialogOptions()
            {
                Resizable = true,
                Draggable = true,
                Width = "700px",
                Height = "512px",
                Left = null,
                Top = null
            });
        if (result != null)
        {
            StateHasChanged();            
        }
    }

    private async void Delete(Permission selectedAuth)
    {
        var result = await DialogService.Confirm("确认要删除吗?", "提示", new ConfirmOptions() 
        { 
            OkButtonText = "确定", 
            CancelButtonText = "取消" 
        });

        if ((bool)result)
        {
            try
            {
                RBACService.DeleteAuth(selectedAuth.Id); 
                auths!.Remove(selectedAuth);
            }
            catch (Exception ex)
            {                
                NotificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "错误", Detail = ex.Message });
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        Query();
    }

}
